package org.hh.netty.server;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextFlow;
import org.hh.netty.entity.User;

import java.net.URL;
import java.util.ResourceBundle;

public class TeacherController implements Initializable {

    @FXML
    VBox vBox;
    @FXML
    ListView userList;
    public ObservableList<User> userObservableList = FXCollections.observableArrayList();
    TeacherModel teacherModel = TeacherModel.getInstance();

    public void initialize(URL location, ResourceBundle resources) {
        userList.setItems(userObservableList);
        userList.setCellFactory(listView -> new ListCell<User>() {
            private ImageView imageView = new ImageView();

            @Override
            protected void updateItem(User user, boolean empty) {
                imageView.setFitWidth(40);
                imageView.setFitHeight(40);
                if (empty) {
                    setText(null);
                    setGraphic(null);
                } else {
                    Image image = user.getImage();
                    imageView.setImage(image);
                    setText(user.getName());
                    setGraphic(imageView);
                }
            }
        });

        teacherModel.setvBox(vBox);
        teacherModel.setUserList(userList);
        teacherModel.setUserObservableList(userObservableList);
        //启动教师端服务
        new Thread(new TeacherServer()).start();
    }
}
